//
//  Drawer.m
//  PopSheet
//
//  Created by G2 on 2021/7/15.
//

#import "Drawer.h"

@interface DrawerCell : UICollectionViewCell<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation DrawerCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(CGRectGetWidth(frame) - 30, 60);
        layout.minimumLineSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"draw"];
        [self.contentView addSubview:_collectionView];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 13;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"draw" forIndexPath:indexPath];
    cell.layer.borderColor = [UIColor colorWithWhite:0.2 alpha:0.5].CGColor;
    cell.layer.borderWidth = 2;
    cell.layer.cornerRadius = 5;
    cell.backgroundColor = [[UIColor brownColor] colorWithAlphaComponent:0.4];
    
    return cell;
}

@end

@interface Drawer()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation Drawer

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
      
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.text = @"Drawer";
        titleLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1];
        titleLabel.frame = CGRectMake(0, 0, CGRectGetWidth(frame), 30);
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        layout.itemSize = CGSizeMake(CGRectGetWidth(frame), CGRectGetHeight(frame) - 50);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, CGRectGetWidth(frame), CGRectGetHeight(frame) - 30) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        [_collectionView registerClass:[DrawerCell class] forCellWithReuseIdentifier:@"draw"];
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        _collectionView.pagingEnabled = true;
        [self addSubview:_collectionView];
      
    }
    return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"draw" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 50);
    self.collectionView.frame = CGRectMake(0, 50, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - 50);;
}

@end
